package com.fangzhi.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component  
public class JwtUtil {

   private static final String key = "964EEA89-7EC5-B9D2-96BA-AAC9C32C67D6";
  
    public static String createToken(String subject) {
        Date now = new Date();  
        Date expiration = new Date(now.getTime() + 2 * 24 * 60 * 60 * 1000); // 7天后过期
        return Jwts.builder()  
                .setSubject(subject)  
                .setIssuedAt(now)  
                .setExpiration(expiration)  
                .signWith(SignatureAlgorithm.HS256, key)
                .compact();  
    }  
  
    public static boolean validateToken(String token) {
        try {  
            Claims claims = Jwts.parser()  
                    .setSigningKey(key)
                    .parseClaimsJws(token)  
                    .getBody();  
            return !claims.getExpiration().before(new Date());
        } catch (Exception e) {  
            return false;  
        }  
    }

    public static void main(String[] args) {
        String token = createToken("123");
        System.out.println(token);
        System.out.println(validateToken("token"));
    }

}